package com.ice.delay.task.core.config;

import com.ice.delay.task.core.helper.TaskInitHelper;
import com.ice.delay.task.core.helper.TaskScheduleHelper;
import com.ice.delay.task.service.DelayTaskDetailService;
import com.ice.delay.task.service.DelayTaskLockService;
import com.ice.delay.task.service.DelayTaskModuleService;
import com.ice.framework.common.util.MiscUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * @author ice
 * @Date 2022/5/14 0014 15:28
 */
@Slf4j
@Component
public class DelayTaskConfig implements InitializingBean, DisposableBean {

    @Autowired
    private DelayTaskDetailService delayTaskDetailService;
    @Autowired
    private DelayTaskModuleService delayTaskModuleService;
    @Autowired
    private DelayTaskLockService delayTaskLockService;
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;
    @Autowired
    private TransactionTemplate transactionTemplate;

    @Value("${delay.task.retryMaxNum}")
    public Integer retryMaxNum;
    @Value("${delay.task.retryIntervalTime}")
    public Integer retryIntervalTime;

    private static DelayTaskConfig delayTaskConfig = null;

    public static DelayTaskConfig getDelayTaskConfig() {
        return delayTaskConfig;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        delayTaskConfig = this;
        // 时间轮
        timerWheel = new TimerWheel(taskExecutor);
        // 程序启动时，执行运行中的任务
        TaskInitHelper.getInstance().start();
        // 启动任务扫描
        TaskScheduleHelper.getInstance().start();
    }

    @Override
    public void destroy() throws Exception {
        timerWheel.stop();
        TaskScheduleHelper.getInstance().stop();
    }


    private TimerWheel timerWheel;

    public DelayTaskDetailService getDelayTaskDetailService() {
        return delayTaskDetailService;
    }

    public DelayTaskModuleService getDelayTaskModuleService() {
        return delayTaskModuleService;
    }

    public DelayTaskLockService getDelayTaskLockService() {
        return delayTaskLockService;
    }

    public ThreadPoolTaskExecutor getTaskExecutor(){
        return taskExecutor;
    }

    public TimerWheel getTimerWheel() {
        return timerWheel;
    }

    public Integer getRetryMaxNum() {
        return retryMaxNum;
    }

    public Integer getRetryIntervalTime() {
        return retryIntervalTime;
    }

    public TransactionTemplate getTransactionTemplate() {
        return transactionTemplate;
    }

    /**
     *
     * @Author wangwei
     * @Date 2022/6/4
     */
    public void executeTransaction(Runnable runnable) {
        transactionTemplate.execute(new TransactionCallback<Boolean>() {
            @Override
            public Boolean doInTransaction(TransactionStatus status) {
                runnable.run();
                return Boolean.TRUE;
            }
        });
    }
    
}
